using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WebBrowser
{
    public partial class HistoryWindow : Window
    {
        private HistoryManager _historyManager;
        public event EventHandler<string>? HistoryItemSelected;

        public HistoryWindow(HistoryManager historyManager)
        {
            InitializeComponent();
            _historyManager = historyManager;
            LoadHistory();
        }

        private void LoadHistory()
        {
            var historyItems = _historyManager.GetHistory();
            HistoryList.ItemsSource = historyItems;
            
            // Show empty state if no history
            EmptyState.Visibility = historyItems.Count == 0 ? Visibility.Visible : Visibility.Collapsed;
            HistoryList.Visibility = historyItems.Count == 0 ? Visibility.Collapsed : Visibility.Visible;
        }

        private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var searchTerm = SearchTextBox.Text;
            var filteredHistory = _historyManager.SearchHistory(searchTerm);
            HistoryList.ItemsSource = filteredHistory;
            
            // Update empty state visibility
            EmptyState.Visibility = filteredHistory.Count == 0 ? Visibility.Visible : Visibility.Collapsed;
            HistoryList.Visibility = filteredHistory.Count == 0 ? Visibility.Collapsed : Visibility.Visible;
        }

        private void HistoryList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (HistoryList.SelectedItem is HistoryItem historyItem)
            {
                HistoryItemSelected?.Invoke(this, historyItem.Url);
                Close();
            }
        }

        private void DeleteHistoryItem_Click(object sender, RoutedEventArgs e)
        {
            if (sender is Button button && button.Tag is HistoryItem historyItem)
            {
                var result = MessageBox.Show($"Are you sure you want to delete '{historyItem.Title}' from history?", 
                                           "Delete History Item", MessageBoxButton.YesNo, MessageBoxImage.Question);
                
                if (result == MessageBoxResult.Yes)
                {
                    _historyManager.RemoveHistoryItem(historyItem.Url);
                    LoadHistory();
                }
            }
        }

        private void ClearHistory_Click(object sender, RoutedEventArgs e)
        {
            var result = MessageBox.Show("Are you sure you want to clear all browsing history?", 
                                       "Clear History", MessageBoxButton.YesNo, MessageBoxImage.Warning);
            
            if (result == MessageBoxResult.Yes)
            {
                _historyManager.ClearHistory();
                LoadHistory();
            }
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}